package auditoryUIbk;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.util.TimerTask;

import javax.swing.*;

public class JTouchpadDebugger extends JComponent {
	
	static java.util.Timer fingerDetector = new java.util.Timer();
	//	for sensing the signals
	static final int miDelay = 100;
	//	 repeat every sec.
	static final int miPeriod = 20;
	static int _x, _y, _z;
	static JComponent UI = null;
	static boolean visible = false;
	
	public static void main(String[] args) {
		JTouchpadDebugger c = new JTouchpadDebugger();
		JFrame f = new JFrame();
		f.getContentPane().add(c);
		UI = c;
		f.pack();
		f.setVisible(true);
		//
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				SimpleStrokeAudioUIbk.terminateTouchPad();
				System.exit(0);
			}
		});
		//
		SimpleStrokeAudioUIbk.initTouchPad();
		fingerDetector.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				int values[] = new int[4];
				SimpleStrokeAudioUIbk.readTouchPadValues(values);
				//System.out.println("x is " + values[0] + " y is " + values[1] + " z is " + values[2] + " button is " + values[3]);
				if (values[0] == _x && values[1] == _y && values[2] == _z)
					visible = false;
				else
					visible = true;
				_x = values[0];
				_y = values[1];
				_z = values[2];
				UI.repaint();
			}
		}, miDelay, miPeriod);
	}
	
	public JTouchpadDebugger() {
		super();
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(500, 500);
	}
	
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setColor(Color.white);
		g2.fill(getBounds());
		float gray = 1-_z/128f;
//		if (visible) {
			g.setColor(new Color(gray, gray, gray));
			g.fillRect(_x/4, _y/4, 10, 10);
			g2.setColor(Color.black);
			g.drawRect(_x/4, _y/4, 10, 10);
//		}
	}
	
	
}
